package com.webside.playbackstatistics.service.impl;

/***
 * lpz 播放统计
 */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import com.itextpdf.text.log.SysoCounter;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.webside.base.baseservice.impl.AbstractService;
import com.webside.playbackstatistics.config.PlayStatisticsConfig;
import com.webside.playbackstatistics.mapper.PlayStatisticsMapper;
import com.webside.playbackstatistics.model.AnaDailyStatEntity;
import com.webside.playbackstatistics.model.AnaSummaryStatEntity;
import com.webside.playbackstatistics.model.AntistopEntity;
import com.webside.playbackstatistics.model.PlayStatisticsEntity;
import com.webside.playbackstatistics.model.SeriesStatEntity;
import com.webside.playbackstatistics.model.StatRankings;
import com.webside.playbackstatistics.service.AnaDailyStatService;
import com.webside.playbackstatistics.service.AnaSummaryStatService;
import com.webside.playbackstatistics.service.DictionaryService;
import com.webside.playbackstatistics.service.PlayStatisticsService;
import com.webside.playbackstatistics.service.SeriesStatService;
import com.webside.playbackstatistics.service.ShareStatService;
import com.webside.resource.model.ResourceEntity;
import com.webside.series.model.Bdictionary;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Service("playStatisticsService")
public class PlayStatisticsServiceImpl extends AbstractService<ResourceEntity, Long> implements PlayStatisticsService {

	@Resource(name = "mongoTemplate")
	private MongoTemplate mongoTemplate;
	@Autowired
	private PlayStatisticsMapper playStatisticsMapper;
	@Autowired
	private AnaDailyStatService anaDailyStatService;
	@Autowired
	private SeriesStatService seriesStatService;
	@Autowired
	private ShareStatService shareStatService;
	@Autowired
	private DictionaryService dictionaryService;

	@Autowired
	private AnaSummaryStatService anaSummaryStatService;

	private DBCollection collection;
	// 设置时间格式
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	private SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
	private SimpleDateFormat dfc = new SimpleDateFormat("yyyy-MM-dd 00:00:00");

	// 查询mongoDB里面播放数据
	@Override
	public List<PlayStatisticsEntity> selectAllPlayStat() {
		// TODO Auto-generated method stub
		// 创建mangodb对象
		collection = mongoTemplate.getCollection(PlayStatisticsConfig.collectionName);
		// System.out.println("ProxyIP记录数：" + collection.count());
		// 查询该表所有的数据
		DBCursor cursor = collection.find();

		// collection

		List<PlayStatisticsEntity> playStatList = new ArrayList<PlayStatisticsEntity>();

		try {
			PlayStatisticsEntity playStat;
			// 循环遍历查出来的数据 并将每条数据添加到PlayStatisticsEntity实体 集合
			if (null != cursor) {
				for (DBObject dbObj : cursor) {
					playStat = new PlayStatisticsEntity();
					// System.out.println(dbObj.toString());
					// 非空判断
					if (null != dbObj.get("_id") && !dbObj.get("_id").equals("")) {
						playStat.setId(dbObj.get("_id").toString());
					}
					if (null != dbObj.get("seriesId") && !dbObj.get("seriesId").equals("")) {
						playStat.setSeriesId(dbObj.get("seriesId").toString());
					}
					playStat.setSeriesName(dbObj.get("seriesName").toString());
					if (null != dbObj.get("seriesTypeCode") && !dbObj.get("seriesTypeCode").equals("")) {
						playStat.setSeriesTypeCode(dbObj.get("seriesTypeCode").toString());
					}
					if (null != dbObj.get("seriesTypeName") && !dbObj.get("seriesTypeName").equals("")) {
						playStat.setSeriesTypeName(dbObj.get("seriesTypeName").toString());
					}
					if (null != dbObj.get("seriesKindCode") && !dbObj.get("seriesKindCode").equals("")) {
						playStat.setSeriesKindCode(dbObj.get("seriesKindCode").toString());
					}
					if (null != dbObj.get("seriesKindName") && !dbObj.get("seriesKindName").equals("")) {
						playStat.setSeriesKindName(dbObj.get("seriesKindName").toString());
					}
					if (null != dbObj.get("browerTime") && !dbObj.get("browerTime").equals("")) {
						playStat.setBrowerTime(dbObj.get("browerTime").toString());
					}
					if (null != dbObj.get("startTime") && !dbObj.get("startTime").equals("")) {
						playStat.setStartTime(dbObj.get("startTime").toString());
					}
					if (null != dbObj.get("endTime") && !dbObj.get("endTime").equals("")) {
						playStat.setEndTime(dbObj.get("endTime").toString());
					}
					if (null != dbObj.get("playDuration") && !dbObj.get("playDuration").equals("")) {
						playStat.setPlayDuration(dbObj.get("playDuration").toString());
					}
					if (null != dbObj.get("source") && !dbObj.get("source").equals("")) {
						playStat.setSource(dbObj.get("source").toString());
					}
					if (null != dbObj.get("ip") && !dbObj.get("ip").equals("")) {
						playStat.setIp(dbObj.get("ip").toString());
					}
					playStatList.add(playStat);

				}
			}
		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			cursor.close();
		}

		// System.out.println(playStatList.size());
		return playStatList;
	}

	// 统计平台今日点播总次数、视频播放总时长、评论数、点赞数、分享数信息。
	@Override
	public AnaDailyStatEntity selectSumCollectStat() {
		// TODO Auto-generated method stub
		collection = mongoTemplate.getCollection(PlayStatisticsConfig.collectionName);
		AnaDailyStatEntity anaDailyStat = new AnaDailyStatEntity();
		// 获取今日日期
		Date newToday = new Date();
		// 获取要统计的时间
		String newTodayTime = df.format(newToday);
		// System.out.println(df.format(newToday));
		// String ceshi = "e7a08";// 測試用的
		Pattern pattern = Pattern.compile("^" + newTodayTime + ".*$", Pattern.CASE_INSENSITIVE);
		BasicDBObject query = new BasicDBObject();
		query.append("browerTime", pattern);
		// DBCursor cursor = collection.find(query);
		DBCursor cursor = collection.find(query);

		// 得到当日记录的总条数 也就是点播次数 并存入统计实体类collectStatisticsEntity
		if (0 != cursor.count()) {
			anaDailyStat.setPlayCount(cursor.count());
		} else {
			anaDailyStat.setPlayCount(0);
		}
		// System.out.println(cursor.count() + "条数据");
		// 循环查到的当天数据并计算播放总时长
		int playTimeSum = 0;
		if (null != cursor) {
			for (DBObject dbObj : cursor) {
				if (null != dbObj.get("playDuration") && !dbObj.get("playDuration").equals("")) {
					playTimeSum += Integer.parseInt(dbObj.get("playDuration").toString());
				}
			}
			if (playTimeSum != 0) {
				playTimeSum = playTimeSum / 60;
			}
			// 得到当天播放总时长 并存入统计实体类collectStatisticsEntity
			if (0 != playTimeSum) {
				anaDailyStat.setPlayTime(playTimeSum);
			} else {
				anaDailyStat.setPlayTime(0);
			}
			// 创建查询今日总评论数和总点赞数用的参数列表
			Map timeAndType = new HashMap();
			// 添加查询今日总评论数的参数
			timeAndType.put("createTime", newTodayTime);
			timeAndType.put("type", "2");
			// 将参数列表集合带入方法中统计今日的总评论数并存入实体类collectStatisticsEntity
			if (0 != this.commentSum(timeAndType)) {
				anaDailyStat.setCommentCount(this.commentSum(timeAndType));
			} else {
				anaDailyStat.setCommentCount(0);

			}
			// 清空参赛列表集合
			timeAndType.clear();
			// 添加查询今日总点赞数的参数
			timeAndType.put("createTime", newTodayTime);
			timeAndType.put("type", "1");
			// 将参数列表集合带入方法中统计今日的总点赞数并存入实体类collectStatisticsEntity
			if (0 != this.commentSum(timeAndType)) {
				anaDailyStat.setLikeCount(this.commentSum(timeAndType));
			} else {
				anaDailyStat.setLikeCount(0);

			}
			// 清空参赛列表集合
			timeAndType.clear();
			// 添加查询今日总浏览数的参数
			timeAndType.put("createTime", newTodayTime);
			timeAndType.put("seriesName", null);
			// 将参数列表集合带入方法中统计今日的总分享数并存入实体类collectStatisticsEntity
			if (0 != shareStatService.shareSum(timeAndType)) {

				anaDailyStat.setShareCount(shareStatService.shareSum(timeAndType));
			} else {
				anaDailyStat.setShareCount(0);
			}
		}

		return anaDailyStat;
	}

	// 这句必须要加上。不然会报空指针异常，因为在实际调用的时候不是BaseMapper调用，而是具体的mapper，这里为userMapper
	@Autowired
	public void setBaseMapper() {
		super.setBaseMapper(playStatisticsMapper);
	}

	// 根据传入的时间查询今日的的总评论数
	@Override
	public int commentSum(Map timeAndType) {
		// TODO Auto-generated method stub
		return playStatisticsMapper.commentSum(timeAndType);
	}
	// 统计平台昨日点播总次数、视频播放总时长、评论数、点赞数、分享数信息。

	// 操作selectSumCollectStat方法

	@Override
	public List<AnaDailyStatEntity> getAnaDailyStatList() {
		// TODO Auto-generated method stub
		// 创建一个list集合接收数据
		List<AnaDailyStatEntity> anaDailyStatList = new ArrayList<AnaDailyStatEntity>();
		// 向集合中添加今日统计数据并注入标示1
		AnaDailyStatEntity newDailyStat1 = new AnaDailyStatEntity();
		
		if (null != this.selectSumCollectStat()) {

			newDailyStat1 = this.selectSumCollectStat();
			newDailyStat1.setMark(1);
			anaDailyStatList.add(newDailyStat1);
		} else {
			newDailyStat1.setMark(1);
			anaDailyStatList.add(newDailyStat1);

		}
		// 向集合中添加昨日统计数据并注入标示2
		// 获取昨日日期
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		String yesterday = df.format(cal.getTime());
		AnaDailyStatEntity newDailyStat2 = new AnaDailyStatEntity();
		if (null != anaDailyStatService.yesterdayStat(yesterday)) {

			newDailyStat2 = anaDailyStatService.yesterdayStat(yesterday);
			newDailyStat2.setMark(2);
			anaDailyStatList.add(newDailyStat2);
		} else {
			newDailyStat2.setMark(2);
			anaDailyStatList.add(newDailyStat2);

		}

		// 向集合中添加历史每日平均统计数据并注入标示3
		AnaDailyStatEntity newDailyStat3 = new AnaDailyStatEntity();
		if (null != anaDailyStatService.avgAnaDailyStat()) {

			newDailyStat3 = anaDailyStatService.avgAnaDailyStat();
			newDailyStat3.setMark(3);
			anaDailyStatList.add(newDailyStat3);

		} else {
			newDailyStat3.setMark(3);
			anaDailyStatList.add(newDailyStat3);

		}
		// 向集合中添加历史峰值统计数据并注入标示4
		AnaDailyStatEntity newDailyStat4 = new AnaDailyStatEntity();
		if (null != anaDailyStatService.maxAnaDailyStat()) {

			newDailyStat4 = anaDailyStatService.maxAnaDailyStat();
			newDailyStat4.setMark(4);
			anaDailyStatList.add(newDailyStat4);

		} else {
			newDailyStat4.setMark(4);
			anaDailyStatList.add(newDailyStat4);

		}

		return anaDailyStatList;
	}

	@Override
	public List<AnaDailyStatEntity> conditionSumCollectStat(String seriesName) {
		// TODO Auto-generated method stub
		collection = mongoTemplate.getCollection(PlayStatisticsConfig.collectionName);
		// 创建一个对象集合
		List<AnaDailyStatEntity> anaDailyStatList = new ArrayList<AnaDailyStatEntity>();
		// 创建一个今日对象实例
		AnaDailyStatEntity newAnaDailyStat = new AnaDailyStatEntity();
		// 创建一个正则格式对象
		Pattern pattern;
		/// 获取当前片子今日的统计信息
		// 获取今日日期
		Date newToday = new Date();
		// 获取要统计的时间
		String newTodayTime = df.format(newToday);
		pattern = Pattern.compile("^" + newTodayTime + ".*$", Pattern.CASE_INSENSITIVE);
		BasicDBObject query = new BasicDBObject();
		query.append("browerTime", pattern);
		query.append("seriesName", seriesName);
		DBCursor cursor = collection.find(query);
		// 得到当日记录的总条数 也就是点播次数 并存入统计实体类collectStatisticsEntity
		if (0 != cursor.count()) {

			newAnaDailyStat.setPlayCount(cursor.count());
		} else {
			newAnaDailyStat.setPlayCount(0);
		}

		// System.out.println(cursor.count() + "条数据");
		// 循环查到的当天数据并计算播放总时长
		int playTimeSum = 0;
		if (null != cursor) {
			for (DBObject dbObj : cursor) {
				if (null != dbObj.get("playDuration") && !dbObj.get("playDuration").equals("")) {
					playTimeSum += Integer.parseInt(dbObj.get("playDuration").toString());
				}
			}
		}
		if (playTimeSum != 0) {
			playTimeSum = playTimeSum / 60;
		}
		// 得到当天播放总时长 并存入统计实体类collectStatisticsEntity
		if (0 != playTimeSum) {

			newAnaDailyStat.setPlayTime(playTimeSum);
		} else {
			newAnaDailyStat.setPlayTime(0);

		}
		// 创建查询今日总评论数和总点赞数用的参数列表
		Map timeTypeName = new HashMap();
		// 添加查询当前片名的今日总评论数的参数
		timeTypeName.put("createTime", newTodayTime);
		timeTypeName.put("type", "2");
		timeTypeName.put("seriesName", seriesName);
		// 将参数列表集合带入方法中统计今日的总评论数并存入实体类collectStatisticsEntity
		if (0 != this.conditionCommentSum(timeTypeName)) {
			newAnaDailyStat.setCommentCount(this.conditionCommentSum(timeTypeName));
		} else {
			newAnaDailyStat.setCommentCount(0);
		}
		// 清空参赛列表集合
		timeTypeName.clear();
		// 添加查询今日总点赞数的参数
		timeTypeName.put("createTime", newTodayTime);
		timeTypeName.put("type", "1");
		timeTypeName.put("seriesName", seriesName);
		// 将参数列表集合带入方法中统计今日的总点赞数并存入实体类collectStatisticsEntity
		if (0 != this.conditionCommentSum(timeTypeName)) {

			newAnaDailyStat.setLikeCount(this.conditionCommentSum(timeTypeName));
		} else {

			newAnaDailyStat.setLikeCount(0);
		}
		// 清空参赛列表集合
		timeTypeName.clear();
		// 添加查询今日总浏览数的参数
		timeTypeName.put("createTime", newTodayTime);
		timeTypeName.put("seriesName", seriesName);
		// 将参数列表集合带入方法中统计今日的总分享数并存入实体类collectStatisticsEntity
		if (0 != shareStatService.shareSum(timeTypeName)) {

			newAnaDailyStat.setShareCount(shareStatService.shareSum(timeTypeName));
		} else {
			newAnaDailyStat.setShareCount(0);
		}

		if (newAnaDailyStat != null) {
			// 插入标示
			newAnaDailyStat.setMark(1);
			anaDailyStatList.add(newAnaDailyStat);
		}

		/// 获取当前片子昨日的统计数据
		// 创建一个今日对象实例
		AnaDailyStatEntity oldAnaDailyStat = new AnaDailyStatEntity();
		// 获取昨日日期
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		String yesterday = df.format(cal.getTime());
		pattern = Pattern.compile("^" + yesterday + ".*$", Pattern.CASE_INSENSITIVE);
		BasicDBObject queryYester = new BasicDBObject();
		queryYester.append("browerTime", pattern);
		queryYester.append("seriesName", seriesName);
		DBCursor cursorYester = collection.find(queryYester);
		// 得到昨日记录的总条数 也就是点播次数 并存入统计实体类collectStatisticsEntity
		if (0 != cursorYester.count()) {
			oldAnaDailyStat.setPlayCount(cursorYester.count());
		} else {
			oldAnaDailyStat.setPlayCount(0);
		}

		// System.out.println(cursorYester.count() + "条数据");
		// 循环查到的昨日数据并计算播放总时长
		int playTimeSumYester = 0;
		if (null != cursorYester) {
			for (DBObject dbObj : cursorYester) {
				if (null != dbObj.get("playDuration") && !dbObj.get("playDuration").equals("")) {
					playTimeSumYester += Integer.parseInt(dbObj.get("playDuration").toString());
				}
			}
		}
		if (playTimeSumYester != 0) {
			playTimeSumYester = playTimeSumYester / 60;
		}
		// 得到昨日播放总时长 并存入统计实体类collectStatisticsEntity
		if (0 != playTimeSumYester) {
			oldAnaDailyStat.setPlayTime(playTimeSumYester);
		} else {
			oldAnaDailyStat.setPlayTime(0);
		}
		// 创建查询昨日总评论数和总点赞数用的参数列表
		timeTypeName.clear();
		// 添加查询当前片名的昨日总评论数的参数
		timeTypeName.put("createTime", yesterday);
		timeTypeName.put("type", "2");
		timeTypeName.put("seriesName", seriesName);
		// 将参数列表集合带入方法中统计今日的总评论数并存入实体类collectStatisticsEntity
		if (0 != this.conditionCommentSum(timeTypeName)) {
			oldAnaDailyStat.setCommentCount(this.conditionCommentSum(timeTypeName));
		} else {
			oldAnaDailyStat.setCommentCount(0);
		}
		// 清空参赛列表集合
		timeTypeName.clear();
		// 添加查询今日总点赞数的参数
		timeTypeName.put("createTime", yesterday);
		timeTypeName.put("type", "1");
		timeTypeName.put("seriesName", seriesName);
		// 将参数列表集合带入方法中统计今日的总点赞数并存入实体类collectStatisticsEntity
		if (0 != this.conditionCommentSum(timeTypeName)) {
			oldAnaDailyStat.setLikeCount(this.conditionCommentSum(timeTypeName));
		} else {
			oldAnaDailyStat.setLikeCount(0);

		}
		// 清空参赛列表集合
		timeTypeName.clear();
		// 添加查询今日总浏览数的参数
		timeTypeName.put("createTime", yesterday);
		timeTypeName.put("seriesName", seriesName);
		// 将参数列表集合带入方法中统计今日的总分享数并存入实体类collectStatisticsEntity
		if (0 != shareStatService.shareSum(timeTypeName)) {

			oldAnaDailyStat.setShareCount(shareStatService.shareSum(timeTypeName));
		} else {
			oldAnaDailyStat.setShareCount(0);
		}

		if (newAnaDailyStat != null) {
			// 插入标示
			oldAnaDailyStat.setMark(2);
			anaDailyStatList.add(oldAnaDailyStat);
		}

		return anaDailyStatList;

	}

	// 根据传入的时间和片名查询今日的的总评论数总评论数和点赞数
	@Override
	public int conditionCommentSum(Map timeTypeName) {
		// TODO Auto-generated method stub
		return playStatisticsMapper.conditionCommentSum(timeTypeName);
	}

	@Override
	public AnaDailyStatEntity cesshi() {
		// TODO Auto-generated method stub

		return null;
	}

	// 根据片名查询到的统计数据
	@Override
	public List<AnaDailyStatEntity> getAnaDailyStatNameList(String seriesName) {
		// TODO Auto-generated method stub
		List<AnaDailyStatEntity> anaDailyStatList = this.conditionSumCollectStat(seriesName);
		AnaDailyStatEntity anaDailyStat;
		if (anaDailyStatList.size() == 2) {
			anaDailyStat = seriesStatService.conditionCommentAvg(seriesName);
			if (anaDailyStat != null) {
				anaDailyStatList.add(anaDailyStat);

			}

		}
		if (anaDailyStatList.size() == 3) {

			anaDailyStat = seriesStatService.conditionCommentMax(seriesName);
			if (anaDailyStat != null) {
				anaDailyStatList.add(anaDailyStat);

			}
		}

		return anaDailyStatList;
	}

	// 根据日期或者片名日期片名查询mongodb里面当天的各个时段的浏览数量
	@Override
	public Map hourCount(Date newDay, String seriesName) {
		collection = mongoTemplate.getCollection(PlayStatisticsConfig.collectionName);
		// TODO Auto-generated method stub\
		// 创建一个map存储要要查询的时间的浏览量和前一天的浏览量
		Map newAndOld = new HashMap();
		// 创建一个正则格式对象
		Pattern pattern;
		// 计算日期前一天的对象
		Calendar cal = Calendar.getInstance();
		// 查询的日期
		Date newToday = new Date();
		// 查询日期前一天的日期
		Date yesterday = new Date();
		// 如果传来的时间为空就统计今天的日期
		if (newDay == null || newDay.equals("")) {
			// 获取今日日期
			newToday = new Date();
			// 获取前一天的日期
			cal.add(Calendar.DATE, -1);
			yesterday = cal.getTime();

		} else {
			// 拿到参数传来的日期
			try {
				newToday = df.parse(df.format(newDay));
			} catch (ParseException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			try {
				cal.setTime(df.parse(df.format(newToday)));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int day = cal.get(Calendar.DATE);
			cal.set(Calendar.DATE, day - 1);
			// 获取前一天的日期
			yesterday = cal.getTime();
			// System.out.println(df.format(yesterday));
		}
		// 新建一个map存储要查询的时间的数据
		// 设置时间格式
		SimpleDateFormat geshi1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat geshi2 = new SimpleDateFormat("HH");
		// 要返回的时间格式
		SimpleDateFormat returnTime = new SimpleDateFormat("yyyy/MM/dd");
		Map<String, Integer> hourCount = new HashMap();
		// 通过循环初始化newData
		for (int i = 0; i <= 9; i++) {
			hourCount.put("today0" + i, 0);
		}
		for (int i = 10; i <= 23; i++) {
			hourCount.put("today" + i + "", 0);
		}
		// 获取初始化map中所有的key
		List<String> keyList = new ArrayList<String>();
		java.util.Iterator it = hourCount.entrySet().iterator();
		while (it.hasNext()) {
			java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
			keyList.add(entry.getKey().toString()); // 返回对应的键
		}
		/// 要查询的当前时间每小时数据
		// 获取要统计的时间
		String newTodayTime = df.format(newToday);
		pattern = Pattern.compile("^" + newTodayTime + ".*$", Pattern.CASE_INSENSITIVE);
		BasicDBObject query = new BasicDBObject();
		query.append("browerTime", pattern);
		if (null != seriesName && !seriesName.equals("")) {

			query.append("seriesName", seriesName);
		}

		DBCursor cursor = collection.find(query);
		int u = cursor.count();
		if (null != cursor) {

			for (DBObject dbObj : cursor) {
				if (null != dbObj.get("browerTime") && !dbObj.get("browerTime").equals("")) {
					String hour = "";
					try {
						hour = "today" + geshi2.format(geshi1.parse(dbObj.get("browerTime").toString()));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// 将查询得到的小时数与map中进行对比来得到count数量
					for (String key : keyList) {
						if (key.equals(hour) || key == hour) {
							hourCount.put(key, Integer.parseInt(hourCount.get(key).toString()) + 1);
							break;
						}

					}

				}

			}
			// System.out.println(hourCount.get("23"));
			// 将要查询日期的一天每个时间段的数据存入map中
			newAndOld.put("newToday", hourCount);
			// 像map中插入前台要显示的当前时间
			newAndOld.put("newTodayTime", newTodayTime);

		}

		/// 要查询的当前时间的前一天数据
		// 创建临时存储数据的map
		Map<String, Integer> hourCount2 = new HashMap();

		// 通过循环初始化hourCount2
		for (int i = 0; i <= 9; i++) {
			hourCount2.put("yesterday0" + i, 0);
		}
		for (int i = 10; i <= 23; i++) {
			hourCount2.put("yesterday" + i + "", 0);
		}
		// 获取初始化map中所有的key
		List<String> keyList2 = new ArrayList<String>();
		java.util.Iterator it2 = hourCount2.entrySet().iterator();
		while (it2.hasNext()) {
			java.util.Map.Entry entry = (java.util.Map.Entry) it2.next();
			keyList2.add(entry.getKey().toString()); // 返回对应的键
		}
		// 获取要统计的时间
		String yesterdayTime = df.format(yesterday);
		pattern = Pattern.compile("^" + yesterdayTime + ".*$", Pattern.CASE_INSENSITIVE);
		BasicDBObject yesterdayQuery = new BasicDBObject();
		yesterdayQuery.append("browerTime", pattern);
		if (null != seriesName && !seriesName.equals("")) {

			yesterdayQuery.append("seriesName", seriesName);
		}

		DBCursor yesterdCursor = collection.find(yesterdayQuery);
		int i = yesterdCursor.count();
		if (null != yesterdCursor) {
			for (DBObject dbObj : yesterdCursor) {
				if (null != dbObj.get("browerTime") && !dbObj.get("browerTime").equals("")) {
					String hour = "";
					try {
						hour = "yesterday" + geshi2.format(geshi1.parse(dbObj.get("browerTime").toString()));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// 将查询得到的小时数与map中进行对比来得到count数量
					for (String key : keyList2) {
						if (key.equals(hour) || key == hour) {
							hourCount2.put(key, Integer.parseInt(hourCount2.get(key).toString()) + 1);
							break;
						}

					}

				}

			}
			// System.out.println(hourCount2.get("23"));
			// 将要查询日期的前一天每个时间段的数据存入map中
			newAndOld.put("yesterday", hourCount2);
			// 像map中插入前台要显示的当前时间的后一天时间
			newAndOld.put("yesterdayTime", yesterdayTime);

		}

		return newAndOld;
	}

	// 根据条件查询剧集播放统计排名
	@Override
	public Map statRankings(StatRankings statRankings) {
		// TODO Auto-generated method stub
		Map rankings = new HashMap();
		List<Bdictionary> dictionary = new ArrayList<Bdictionary>();
		List<StatRankings> kindList = new ArrayList<StatRankings>();// 获取剧集列表
		List<StatRankings> typeList = new ArrayList<StatRankings>();// 获取剧情列表

		if (!statRankings.getChannel().equals(PlayStatisticsConfig.channel)) {
			// 获取当前频道的剧集
			String kind = PlayStatisticsConfig.kind;
			// List<StatRankings> kindList = new ArrayList();

			dictionary = dictionaryService.dictionaryList(kind, statRankings.getChannel());
			if (null != dictionary) {
				for (Bdictionary bd : dictionary) {
					StatRankings st = new StatRankings();
					if (null != bd) {

						st.setChannel(statRankings.getChannel());
						st.setKind(bd.getCode());
						st.setName(bd.getName());
					}

					kindList.add(st);

				}

			}

			// 获取当前频道的剧情
			String type = PlayStatisticsConfig.type;
			// List<StatRankings> typeList = new ArrayList();
			dictionary = dictionaryService.dictionaryList(type, statRankings.getChannel());
			if (null != dictionary) {
				for (Bdictionary bd : dictionary) {
					StatRankings st = new StatRankings();
					if (null != bd) {
						st.setChannel(statRankings.getChannel());
						st.setType(bd.getCode());
						st.setName(bd.getName());
					}

					typeList.add(st);

				}

			}

		}

		// 获取当前频道和当前剧集类型当前剧情类型时间范围这些参数 在数据库中查询到对应的数据前10条数据
		List<AnaSummaryStatEntity> anaSummaryStatList = new ArrayList<AnaSummaryStatEntity>();
		// 参数列表
		AnaSummaryStatEntity anaSummary = new AnaSummaryStatEntity();
		anaSummary.setSeriesKindCode(statRankings.getKind());
		anaSummary.setDramaTypeCode(statRankings.getType());

		// 将数据处理后存入rankingList供前台显示
		List<StatRankings> rankingList = new ArrayList<StatRankings>();

		// 传入统计的参数如果为全频道就不传kind和type
		anaSummary.setMark(statRankings.getFrame().trim());
		if (!statRankings.getChannel().equals(PlayStatisticsConfig.channel)) {

			if (null != statRankings.getKind() && !statRankings.getKind().equals("")) {
				anaSummary.setSeriesKindCode(statRankings.getKind());
				anaSummary.setDramaTypeCode(statRankings.getType());
			} else {

				anaSummary.setSeriesKindCode(statRankings.getChannel());
				anaSummary.setDramaTypeCode(statRankings.getChannel());
			}

		}
		anaSummaryStatList = anaSummaryStatService.selectAnaSummaryStatAll(anaSummary);
		int i = 1;// 存储排行信息
		switch (statRankings.getFrame().trim()) {
		// 查询今日统计排行
		case "日":
			for (AnaSummaryStatEntity ase : anaSummaryStatList) {
				StatRankings srk = new StatRankings();
				srk.setSeriesName(ase.getSeriesName());
				srk.setRankings(i);
				srk.setPlayCount(ase.getLastdayPlayCount().toString());
				srk.setRisingIndex(ase.getDayGoupExp());
				rankingList.add(srk);
				i++;
			}

			break;
		case "周":
			for (AnaSummaryStatEntity ase : anaSummaryStatList) {
				StatRankings srk = new StatRankings();
				srk.setSeriesName(ase.getSeriesName());
				srk.setRankings(i);
				srk.setPlayCount(ase.getLastweekPlayCount().toString());
				srk.setRisingIndex(ase.getWeekGoupExp());
				rankingList.add(srk);
				i++;
			}
			break;
		case "月":
			for (AnaSummaryStatEntity ase : anaSummaryStatList) {
				StatRankings srk = new StatRankings();
				srk.setSeriesName(ase.getSeriesName());
				srk.setRankings(i);
				srk.setPlayCount(ase.getLastmonthPlayCount().toString());
				srk.setRisingIndex(ase.getMonthGoupExp());
				rankingList.add(srk);
				i++;
			}
			break;
		case "总":
			for (AnaSummaryStatEntity ase : anaSummaryStatList) {
				StatRankings srk = new StatRankings();
				srk.setSeriesName(ase.getSeriesName());
				srk.setRankings(i);
				srk.setPlayCount(ase.getLastdayEndPlayCount().toString());
				rankingList.add(srk);
				i++;
			}
			break;
		}
		rankings.put("kind", kindList);// 插入剧集下拉框信息
		rankings.put("type", typeList);// 插入剧情下拉框信息
		rankings.put("ranking", rankingList);// 插入排行信息
		return rankings;
	}

	// 统计参数
	Criteria criteria = null;
	// mongodb统计的数据

	// 当前统计的排行信息
	private List<AnaSummaryStatEntity> rankingList = null;

	// 昨日排行：上一日（0:00-23:59）的剧集点播次数排行；
	// 昨日点播：上一日（0:00-23:59）的剧集点播次数；
	@Override
	public List<AnaSummaryStatEntity> yesterdayWas() {
		rankingList = new ArrayList<AnaSummaryStatEntity>();
		// 获取昨日日期
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		String yesterday = df.format(cal.getTime());
		Pattern pattern = Pattern.compile("^" + yesterday + ".*$", Pattern.CASE_INSENSITIVE);
		// 查询参数
		criteria = Criteria.where("browerTime").regex(pattern);
		// 调用公用 查询方法
		rankingList = this.selectRanking(criteria).getMappedResults();

		// 将统计出来的昨日点播次数 插入排名 和播放次数
		for (int i = 1; i <= rankingList.size(); i++) {
			rankingList.get(i - 1).setLastdayRank(i);
			rankingList.get(i - 1).setLastdayPlayCount(rankingList.get(i - 1).getPlayCount());

		}
		// TODO Auto-generated method stub
		rankingList = this.anaSummaryStatList(rankingList, "日");
		return rankingList;
	}

	// 上周点播：当前时间的上一自然周（周一－周日）的点播次数；
	// 上周排行：当前时间的上一自然周（周一－周日）的点播次数排行；
	@Override
	public List<AnaSummaryStatEntity> lastWeekRanking() {
		// TODO Auto-generated method stub
		rankingList = new ArrayList<AnaSummaryStatEntity>();
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, 1);
		// 上周日
		String lS = dfs.format(cal.getTime());
		// System.out.println("上周日为：" + lS);
		cal.add(Calendar.WEEK_OF_MONTH, -1);
		cal.set(Calendar.DAY_OF_WEEK, 2);
		// 上周一
		String lM = dfc.format(cal.getTime());
		// System.out.println("上周一为：" + lM);

		// 查询参数
		criteria = Criteria.where("browerTime").lte(lS).gte(lM);
		// 调用公用 查询方法
		rankingList = this.selectRanking(criteria).getMappedResults();
		for (int i = 1; i <= rankingList.size(); i++) {
			rankingList.get(i - 1).setLastweekRank(i);
			rankingList.get(i - 1).setLastweekPlayCount(rankingList.get(i - 1).getPlayCount());
		}
		rankingList = this.anaSummaryStatList(rankingList, "周");
		return rankingList;
	}

	// 截止到上周末点播
	// 截止到上周末排行
	@Override
	public List<AnaSummaryStatEntity> lastWeekClose() {
		// TODO Auto-generated method stub
		rankingList = new ArrayList<AnaSummaryStatEntity>();
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, 1);
		// 上周日
		String lS = dfs.format(cal.getTime());
		// 查询参数
		criteria = Criteria.where("browerTime").lte(lS);
		// 调用公用 查询方法
		rankingList = this.selectRanking(criteria).getMappedResults();
		for (int i = 1; i <= rankingList.size(); i++) {
			rankingList.get(i - 1).setLastweekEndRank(i);
			rankingList.get(i - 1).setLastweekEndPlayCount(rankingList.get(i - 1).getPlayCount());
		}
		rankingList = this.anaSummaryStatList(rankingList, "截止周");
		return rankingList;

	}

	// 上月点播：当前时间的上一自然月（1- 30号）的点播次数；
	// 上月排行：当前时间的上一自然月（1-30号）的点播次数排行；
	@Override
	public List<AnaSummaryStatEntity> LastMonthRanking() {
		// TODO Auto-generated method stub
		// 获取上个月的时间
		SimpleDateFormat lastMonth = new SimpleDateFormat("yyyy-MM");
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.add(Calendar.MONTH, -1);
		startCalendar.set(Calendar.DAY_OF_MONTH, 1);
		String month = lastMonth.format(startCalendar.getTime());
		Pattern patternMonth = Pattern.compile("^" + month + ".*$", Pattern.CASE_INSENSITIVE);
		criteria = Criteria.where("browerTime").regex(patternMonth);
		rankingList = this.selectRanking(criteria).getMappedResults();
		for (int i = 1; i <= rankingList.size(); i++) {
			rankingList.get(i - 1).setLastmonthRank(i);
			rankingList.get(i - 1).setLastmonthPlayCount(rankingList.get(i - 1).getPlayCount());
		}
		rankingList = this.anaSummaryStatList(rankingList, "月");
		return rankingList;
	}

	// 截止到上月点播
	// 截止到上月排行
	@Override
	public List<AnaSummaryStatEntity> LastMonthClose() {
		// 获取上个月的时间
		// SimpleDateFormat lastMonth = new SimpleDateFormat("yyyy-MM");
		Calendar startCalendar = Calendar.getInstance();
		// startCalendar.add(Calendar.MONTH, -1);
		startCalendar.set(Calendar.DAY_OF_MONTH, 0);
		String month = dfs.format(startCalendar.getTime());
		// 查询参数
		criteria = Criteria.where("browerTime").lte(month);
		rankingList = this.selectRanking(criteria).getMappedResults();
		for (int i = 1; i <= rankingList.size(); i++) {
			rankingList.get(i - 1).setLastmonthEndRank(i);
			rankingList.get(i - 1).setLastmonthEndPlayCount(rankingList.get(i - 1).getPlayCount());
		}
		rankingList = this.anaSummaryStatList(rankingList, "截止月");
		return rankingList;
	}

	// 总点播：截止到昨日（23:59）的点播总次数；
	// 总排行：截止到昨日（23:59）的点播排行；
	@Override
	public List<AnaSummaryStatEntity> yesterdayWasClose() {
		// TODO Auto-generated method stub
		rankingList = new ArrayList<AnaSummaryStatEntity>();
		// 获取昨日日期
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		String yesterday = dfs.format(cal.getTime());
		// 查询参数
		criteria = Criteria.where("browerTime").lte(yesterday);
		// 调用公用 查询方法
		rankingList = this.selectRanking(criteria).getMappedResults();

		// 将统计出来的昨日点播次数 插入排名 和播放次数
		for (int i = 1; i <= rankingList.size(); i++) {
			rankingList.get(i - 1).setLastdayEndRank(i);
			rankingList.get(i - 1).setLastdayEndPlayCount(rankingList.get(i - 1).getPlayCount());

		}
		rankingList = this.anaSummaryStatList(rankingList, "截止日");
		return rankingList;
	}

	// 获取计算上升指数要对比旧的的数据
	private List<AnaSummaryStatEntity> oldList = new ArrayList<AnaSummaryStatEntity>();
	// 获取计算上升指数要对比新的的数据
	private List<AnaSummaryStatEntity> newList = new ArrayList<AnaSummaryStatEntity>();

	// 日上升指数：＝前一日排行－昨日排行
	@Override
	public List<AnaSummaryStatEntity> dayUpIndex() {
		// TODO Auto-generated method stub
		// 获取前天日期
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -2);
		String yesterday = df.format(cal.getTime());
		Pattern pattern = Pattern.compile("^" + yesterday + ".*$", Pattern.CASE_INSENSITIVE);
		// 查询参数
		criteria = Criteria.where("browerTime").regex(pattern);
		// 调用公用 查询方法获取旧的数据
		oldList = this.selectRanking(criteria).getMappedResults();
		// 将统计出来的前日点播次数 插入排名 和播放次数
		for (int i = 1; i <= oldList.size(); i++) {
			oldList.get(i - 1).setLastdayRank(i);
			oldList.get(i - 1).setLastdayPlayCount(oldList.get(i - 1).getPlayCount());

		}
		// 获取昨日数据
		newList = this.yesterdayWas();
		// 调用 工具方法将两组数据进行比教算出 上升指数
		newList = this.complementRanking(newList, oldList, "日");

		return newList;
	}

	// 周上升指数（最近一周）＝当前8日前排行－总排行
	@Override
	public List<AnaSummaryStatEntity> WeekUpIndex() {

		// 获取8日前的日期
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -8);
		String weekDay = dfc.format(cal.getTime());
		// System.out.println("8天前" + weekDay);
		// 查询参数
		criteria = Criteria.where("browerTime").lt(weekDay);
		// 调用公用 查询方法获取旧的数据
		oldList = this.selectRanking(criteria).getMappedResults();
		// 将统计出来的前日点播次数 插入排名 和播放次数
		for (int i = 1; i <= oldList.size(); i++) {
			oldList.get(i - 1).setLastdayRank(i);
			oldList.get(i - 1).setLastdayPlayCount(oldList.get(i - 1).getPlayCount());

		}
		// 获取昨日数据
		newList = this.yesterdayWasClose();
		// 调用 工具方法将两组数据进行比教算出 上升指数
		newList = this.complementRanking(newList, oldList, "周");

		return newList;

	}

	// 月上升指数（最近一月）＝当前30日前排行－总排行
	@Override
	public List<AnaSummaryStatEntity> MonthUpIndex() {
		// 获取31日前的日期
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -31);
		String monthDay = dfc.format(cal.getTime());
		// System.out.println("31天前" + monthDay);
		// 查询参数
		criteria = Criteria.where("browerTime").lt(monthDay);
		// 调用公用 查询方法获取旧的数据
		oldList = this.selectRanking(criteria).getMappedResults();
		// 将统计出来的前日点播次数 插入排名 和播放次数
		for (int i = 1; i <= oldList.size(); i++) {
			oldList.get(i - 1).setLastdayRank(i);
			oldList.get(i - 1).setLastdayPlayCount(oldList.get(i - 1).getPlayCount());

		}
		// 获取截至所日的总数据数据
		newList = this.yesterdayWasClose();
		// 调用 工具方法将两组数据进行比教算出 上升指数
		newList = this.complementRanking(newList, oldList, "月");

		return newList;
	}

	// 传递参数到mogodb查询播放排行信息
	public AggregationResults<AnaSummaryStatEntity> selectRanking(Criteria criteria) {
		// TODO Auto-generated method stub
		AggregationResults<AnaSummaryStatEntity> gorupResults = null;
		Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria),
				Aggregation.group("seriesId").count().as("playCount").last("seriesName").as("seriesName")
						.last("browerTime").as("browerTime").last("seriesTypeCode").as("dramaTypeCode")
						.last("seriesKindCode").as("seriesKindCode").last("seriesId").as("seriesId"),
				Aggregation.project("playCount", "seriesName", "browerTime", "dramaTypeCode", "seriesKindCode")
						.and("seriesId").previousOperation(),
				Aggregation.sort(Sort.Direction.DESC, "playCount"));
		gorupResults = mongoTemplate.aggregate(agg, "play_records", AnaSummaryStatEntity.class);

		return gorupResults;
	}

	// 获取所有剧集信息补全当天没有浏览量的剧集并算出上升指數
	public List<AnaSummaryStatEntity> complementRanking(List<AnaSummaryStatEntity> newRankingList,
			List<AnaSummaryStatEntity> oldRankingList, String dimensionality) {
		// 将传来的数据处理
		List tempList = Arrays.asList(newRankingList.toArray());
		// 新的数据
		newRankingList = new ArrayList<AnaSummaryStatEntity>(tempList);

		List tempList2 = Arrays.asList(oldRankingList.toArray());
		// 与新数据进行比较的旧数据
		oldRankingList = new ArrayList<AnaSummaryStatEntity>(tempList2);

		oldRankingList = this.anaSummaryStatList(oldRankingList, "日");
		// 获取所有剧集信息
		List<SeriesStatEntity> seriesList = seriesStatService.getSelectAllList();

		// 用旧的排名数据减去新的排名算出上升指数
		for (AnaSummaryStatEntity oldRanking : oldRankingList) {
			int id1 =0;
			if(oldRanking.getSeriesId()!=null){
			 id1 = oldRanking.getSeriesId();
			 }
			for (AnaSummaryStatEntity newRanking : newRankingList) {
				int id2 =0;
				if(newRanking.getSeriesId()!=null){ 
				id2= newRanking.getSeriesId();
				}
				if (id1 == id2) {
					// System.out.println(id1 + "idididididididi" + id2);
					// System.out.println(oldRanking.getSeriesName() +
					// "namenamename" + newRanking.getSeriesName());
					int top1;
					int top2;
					switch (dimensionality.trim()) {
					case "日":
						// System.out.println("日");
						// 前一日
						top1 = oldRanking.getLastdayRank();
						// 昨日排名
						top2 = newRanking.getLastdayRank();
						// System.out.println(top1 + "日日日" + top2);
						newRanking.setDayGoupExp(top1 - top2);
						// System.out.println("日 "+newRanking.getDayGoupExp());
						break;

					case "周":
						// System.out.println("周");
						// 当前时间前8日前排行
						top1 = oldRanking.getLastdayRank();
						// 当前时间总排名
						top2 = newRanking.getLastdayEndRank();
						// System.out.println(top1 + "周周周" + top2);
						newRanking.setWeekGoupExp(top1 - top2);
						// System.out.println("周 "+newRanking.getWeekGoupExp());
						break;

					case "月":
						// System.out.println("月");
						// 当前时间前30日前排行
						top1 = oldRanking.getLastdayRank();
						// 当前时间总排名
						top2 = newRanking.getLastdayEndRank();
						// System.out.println(top1 + "月月月" + top2);
						newRanking.setMonthGoupExp(top1 - top2);
						// System.out.println("月
						// "+newRanking.getMonthGoupExp());
						break;

					}
					break;
				} else {

					continue;
				}

			}

		}

		return newRankingList;

	}

	// 将查到的排行信息没有排行的聚集排行设为默认最大值
	public List<AnaSummaryStatEntity> anaSummaryStatList(List<AnaSummaryStatEntity> RankingList,
			String dimensionality) {

		// 将传来的数据处理
		List tempList = Arrays.asList(RankingList.toArray());
		// 新的数据
		RankingList = new ArrayList<AnaSummaryStatEntity>(tempList);
		// System.out.println("新新新" + RankingList.size() + dimensionality);
		// for (AnaSummaryStatEntity a : RankingList) {
		// System.out.println("新Id" + a.getSeriesId() + "新name" +
		// a.getSeriesName());
		// }

		// 获取所有剧集信息
		List<SeriesStatEntity> seriesList = seriesStatService.getSelectAllList();
		// 如果某个剧集在当前时间段没有被观看过 也就没有排名 那默认为最后一名播放量为0
		int top = seriesList.size();
		for (SeriesStatEntity sse : seriesList) {
			int id = sse.getId();
			int index = 0;
			for (AnaSummaryStatEntity ass : RankingList) {
				int id2=0;
				if(ass.getSeriesId()!=null){
				id2 = ass.getSeriesId();
				}
				if (id == id2) {
					index = 1;
					break;
				} else {

					continue;
				}

			}

			if (index == 0) {
				AnaSummaryStatEntity anaSummary = new AnaSummaryStatEntity();
				switch (dimensionality.trim()) {
				case "日":
					anaSummary.setSeriesId(sse.getId());
					anaSummary.setSeriesName(sse.getName());
					anaSummary.setLastdayRank(top);
					anaSummary.setLastdayPlayCount(0);
					RankingList.add(anaSummary);
					break;
				case "截止日":
					anaSummary.setSeriesId(sse.getId());
					anaSummary.setSeriesName(sse.getName());
					anaSummary.setLastdayEndRank(top);
					anaSummary.setLastdayEndPlayCount(0);
					RankingList.add(anaSummary);
					break;

				case "周":
					anaSummary.setSeriesId(sse.getId());
					anaSummary.setSeriesName(sse.getName());
					anaSummary.setLastweekRank(top);
					anaSummary.setLastweekPlayCount(0);
					RankingList.add(anaSummary);
					break;

				case "截止周":
					anaSummary.setSeriesId(sse.getId());
					anaSummary.setSeriesName(sse.getName());
					anaSummary.setLastweekEndRank(top);
					anaSummary.setLastweekEndPlayCount(0);
					RankingList.add(anaSummary);
					break;
				case "月":
					anaSummary.setSeriesId(sse.getId());
					anaSummary.setSeriesName(sse.getName());
					anaSummary.setLastmonthRank(top);
					anaSummary.setLastmonthPlayCount(0);
					RankingList.add(anaSummary);
					break;

				case "截止月":
					anaSummary.setSeriesId(sse.getId());
					anaSummary.setSeriesName(sse.getName());
					anaSummary.setLastmonthEndRank(top);
					anaSummary.setLastmonthEndPlayCount(0);
					RankingList.add(anaSummary);
					break;

				}

			}

		}

		return RankingList;
	}

	// 统计平台昨日点播总次数、视频播放总时长、评论数、点赞数、分享数信息。
	@Override
	public AnaDailyStatEntity yesterdaySumCollectStat() {
		// TODO Auto-generated method stub
		collection = mongoTemplate.getCollection(PlayStatisticsConfig.collectionName);
		AnaDailyStatEntity anaDailyStat = new AnaDailyStatEntity();
		// 获取昨日日期
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		// 获取要统计的时间
		String yesterdayTime = df.format(cal.getTime());
		// System.out.println(df.format(newToday));
		// String ceshi = "e7a08";// 測試用的
		Pattern pattern = Pattern.compile("^" + yesterdayTime + ".*$", Pattern.CASE_INSENSITIVE);
		BasicDBObject query = new BasicDBObject();
		query.append("browerTime", pattern);
		// DBCursor cursor = collection.find(query);
		DBCursor cursor = collection.find(query);

		// 得到当日记录的总条数 也就是点播次数 并存入统计实体类collectStatisticsEntity
		if (0 != cursor.count()) {
			anaDailyStat.setPlayCount(cursor.count());
		} else {
			anaDailyStat.setPlayCount(0);
		}
		// System.out.println(cursor.count() + "条数据");
		// 循环查到的昨日数据并计算播放总时长
		int playTimeSum = 0;
		if (null != cursor) {
			for (DBObject dbObj : cursor) {
				if (null != dbObj.get("playDuration") && !dbObj.get("playDuration").equals("")) {
					playTimeSum += Integer.parseInt(dbObj.get("playDuration").toString());
				}
			}
			if (playTimeSum != 0) {
				playTimeSum = playTimeSum / 60;
			}
			// 得到昨日播放总时长 并存入统计实体类collectStatisticsEntity
			if (0 != playTimeSum) {
				anaDailyStat.setPlayTime(playTimeSum);
			} else {
				anaDailyStat.setPlayTime(0);
			}
			// 创建查询昨日总评论数和总点赞数用的参数列表
			Map timeAndType = new HashMap();
			// 添加查询昨日总评论数的参数
			timeAndType.put("createTime", yesterdayTime);
			timeAndType.put("type", "2");
			// 将参数列表集合带入方法中统计今日的总评论数并存入实体类collectStatisticsEntity
			if (0 != this.commentSum(timeAndType)) {
				anaDailyStat.setCommentCount(this.commentSum(timeAndType));
			} else {
				anaDailyStat.setCommentCount(0);

			}
			// 清空参赛列表集合
			timeAndType.clear();
			// 添加查询昨日总点赞数的参数
			timeAndType.put("createTime", yesterdayTime);
			timeAndType.put("type", "1");
			// 将参数列表集合带入方法中统计今日的总点赞数并存入实体类collectStatisticsEntity
			if (0 != this.commentSum(timeAndType)) {
				anaDailyStat.setLikeCount(this.commentSum(timeAndType));
			} else {
				anaDailyStat.setLikeCount(0);

			}
			// 清空参赛列表集合
			timeAndType.clear();
			// 添加查询昨日总浏览数的参数
			timeAndType.put("createTime", yesterdayTime);
			timeAndType.put("seriesName", null);
			// 将参数列表集合带入方法中统计今日的总分享数并存入实体类collectStatisticsEntity
			if (0 != shareStatService.shareSum(timeAndType)) {

				anaDailyStat.setShareCount(shareStatService.shareSum(timeAndType));
			} else {
				anaDailyStat.setShareCount(0);
			}
		}

		return anaDailyStat;
	}

	// 关键词搜索次数统计排行
	@Override
	public List<AntistopEntity> antistopRanking(String frame) {
		// TODO Auto-generated method stub
		List<AntistopEntity> antistopList = new ArrayList<AntistopEntity>();
		collection = mongoTemplate.getCollection(PlayStatisticsConfig.collectionName2);
		DBCursor cursor = null;
		// 得到总记录条数作统计占比时使用
		double count = 0;
		// 统计参数
		BasicDBObject query = new BasicDBObject();
		Criteria criteria2 = null;
		switch (frame.trim()) {

		case "日":
			// 获取昨日日期
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, -1);
			String yesterday = df.format(cal.getTime());

			Pattern pattern = Pattern.compile("^" + yesterday + ".*$", Pattern.CASE_INSENSITIVE);
			// 查询昨日的记录总条数
			query.append("searchTime", pattern);
			cursor = collection.find(query);
			count = cursor.count();
			// 查询参数
			criteria2 = Criteria.where("searchTime").regex(pattern);
			// 调用公用 查询方法

			break;
		case "周":
			Calendar cal2 = Calendar.getInstance();
			cal2.set(Calendar.DAY_OF_WEEK, 1);
			// 上周日
			String lS = dfs.format(cal2.getTime());
			// System.out.println("上周日为：" + lS);
			cal2.add(Calendar.WEEK_OF_MONTH, -1);
			cal2.set(Calendar.DAY_OF_WEEK, 2);
			// 上周一
			String lM = dfc.format(cal2.getTime());
			// System.out.println("上周一为：" + lM);
			// 查询参数
			criteria2 = Criteria.where("searchTime").lte(lS).gte(lM);
			//上个自然周一共有多少天数据
			List<AntistopEntity> wheekCount= new ArrayList<AntistopEntity>();
			wheekCount = this.selectWeekCount(criteria2).getMappedResults();
			count = 0;
			for(AntistopEntity ae:wheekCount){
				count += (double)ae.getCount();
			}

			break;
		case "月":

			SimpleDateFormat lastMonth = new SimpleDateFormat("yyyy-MM");
			Calendar startCalendar = Calendar.getInstance();
			startCalendar.add(Calendar.MONTH, -1);
			startCalendar.set(Calendar.DAY_OF_MONTH, 1);
			String month = lastMonth.format(startCalendar.getTime());
			Pattern patternMonth = Pattern.compile("^" + month + ".*$", Pattern.CASE_INSENSITIVE);

			// 查询昨日的记录总条数
			query.append("searchTime", patternMonth);
			cursor = collection.find(query);
			count = cursor.count();
			// 查询参数
			criteria2 = Criteria.where("searchTime").regex(patternMonth);
			break;
		case "总":
			// 截止到昨日23.59.59
			// 获取昨日日期
			Calendar cal4 = Calendar.getInstance();
			cal4.add(Calendar.DATE, -1);
			String yesterday2 = dfs.format(cal4.getTime());

			// 查询参数
			criteria2 = Criteria.where("searchTime").lte(yesterday2);
			//截止昨天一共有多少数据
			List<AntistopEntity> sumCount= new ArrayList<AntistopEntity>();
			sumCount = this.selectWeekCount(criteria2).getMappedResults();
			count = 0;
			for(AntistopEntity ae:sumCount){
				count += (double)ae.getCount();
			}

			break;

		}

		antistopList = this.selectAntistopRanking(criteria2).getMappedResults();
		List tempList = Arrays.asList(antistopList.toArray());
		// 新的数据
		antistopList = new ArrayList<AntistopEntity>(tempList);
		// 将统计出来的昨日点播
		// double保留小数点后两位
		DecimalFormat dec = new DecimalFormat("######0.00");
		for (int i = 1; i <= antistopList.size(); i++) {
			antistopList.get(i - 1).setAntistopRanking(i);
			double a = antistopList.get(i - 1).getCount();
			double mach = a / count * 100;
			String proportion = String.valueOf(dec.format(mach)) + "%";
			antistopList.get(i - 1).setProportion(proportion);
		}

		return antistopList;
	}

	// 传递参数到mogodb查询关键词排行信息
	public AggregationResults<AntistopEntity> selectAntistopRanking(Criteria criteria) {
		// TODO Auto-generated method stub
		AggregationResults<AntistopEntity> gorupResults = null;
		Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria),
				Aggregation.group("searchWord").count().as("count").last("searchWord").as("searchWord")
						.last("searchTime").as("searchTime").last("ip").as("ip"),
				Aggregation.project("count", "searchTime", "ip").and("searchWord").previousOperation(),
				Aggregation.sort(Sort.Direction.DESC, "count"),
				Aggregation.limit(10));
		gorupResults = mongoTemplate.aggregate(agg, PlayStatisticsConfig.collectionName2, AntistopEntity.class);

		return gorupResults;

	}
	
	// 传递参数到mogodb查询上个自然周的记录总数量信息
	public AggregationResults<AntistopEntity> selectWeekCount(Criteria criteria) {
		// TODO Auto-generated method stub
		AggregationResults<AntistopEntity> gorupResults = null;
		Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria),
				Aggregation.group("searchWord").count().as("count").last("searchWord").as("searchWord")
						.last("searchTime").as("searchTime").last("ip").as("ip"),
				Aggregation.project("count", "searchTime", "ip").and("searchWord").previousOperation(),
				Aggregation.sort(Sort.Direction.DESC, "count"));
		gorupResults = mongoTemplate.aggregate(agg, PlayStatisticsConfig.collectionName2, AntistopEntity.class);

		return gorupResults;

	}

}
